//
//  FileListViewCell.swift
//  Browser
//
//  Created by Garenge on 2021/7/20.
//

import UIKit

class FileListViewCell: UICollectionViewCell {

    public var fileModel: FileListModel? {
        didSet {

            guard let fileModel = fileModel else { return }
            guard let fileName = fileModel.fileName else { return }
            guard let filePath = self.filePathClosure?(fileName) else { return }

            self.titleLabel.text = fileName

            let fileType = KFileManager.getFileType(withFileName: fileName)

            switch fileType {
            case .picture:
                self.imageView.kf.setImage(with: URL(fileURLWithPath: filePath), placeholder: UIImage(named: "file_type_v_image"))
            case .text:
                self.imageView.image = UIImage(named: "file_type_v_txt")
            case .archive:
                self.imageView.image = UIImage(named: "file_type_v_zip")
            case .folder:
                self.imageView.image = UIImage(named: "file_type_v_folder")
            case .other:
                self.imageView.image = UIImage(named: "file_type_v_unknown")
            }
        }
    }

    var filePathClosure:((_ fileName: String) -> (_: String))?

    var imageView = UIImageView()

    private var titleLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let bgView = UIView()
        bgView.backgroundColor = KDefine.Color.white
        bgView.layer.cornerRadius = 8;
        bgView.layer.masksToBounds = true
        self.contentView.addSubview(bgView)

        bgView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.zero)
        }

        contentView.layer.shadowColor = KDefine.Color.simpleColor(0, 0, 0, 0.1).cgColor;
        contentView.layer.shadowOffset = CGSize(width: 0, height: 2);
        contentView.layer.shadowOpacity = 1;
        contentView.layer.shadowRadius = 7;

        imageView.contentMode = .scaleAspectFill
        bgView.addSubview(imageView)

        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(4)
            make.left.equalToSuperview().offset(4);
            make.right.equalToSuperview().offset(-4);
            make.height.equalTo(imageView.snp.width)
        }

        titleLabel.textColor = KDefine.Color.simpleColor(102, 102, 102)
        titleLabel.font = KDefine.Font.system15;
        titleLabel.numberOfLines = 3
        titleLabel.textAlignment = .center
        self.contentView.addSubview(titleLabel)

        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(4);
            make.right.equalToSuperview().offset(-4);
            make.bottom.equalToSuperview().offset(-4);
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
